﻿using STHPMS1.BLL.B_Core;
using STHPMS1.Common.C_Entity.Stmpms1;
using STHPMS1.ICore;
using STHPMS1.ICore.I_BLL.Bus;
using STHPMS1.ICore.I_DAL;
using STHPMS1.ICore.I_Entity.Info;
using STHPMS1.ICore.I_Entity.Stmpms1;
using STHPMS1.ICore.I_Serv;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq.Expressions;

namespace STHPMS1.BLL.B_Bus
{
    public class Dr_Ini_StruBus : BllBase, IDr_Ini_StruBill
    {
        public void Save<T>(T entity)
            where T : class ,IDr_Ini_Stru
        {
            this.Scene.Dal.Sthpms1.Save(entity);
        }

        public void Remove(IDr_Ini_Stru entity)
        {
            this.Scene.Dal.Sthpms1.Delete(entity);
        }

        public IEnumerable<T> Get<T>(Expression<Func<T, bool>> predicate, IPagingInfo info)
            where T : class, STHPMS1.ICore.I_Entity.IEntity
        {
            return this.Scene.Dal.Sthpms1.Select<T>(predicate, info);
        }


        public void SaveThinkness(string iniproj_Id, int medi_Maintype, Dictionary<int, double> dict)
        {
            ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
            using (IDbTransaction dbTrans = dao.BeginTransaction())
            {
                dao.Update<Dr_Largemeas_Ini>(new { Medi_Maintype = medi_Maintype }, p => p.Iniproj_Id == iniproj_Id);
                dao.Delete<Dr_Ini_Stru>(p => p.Iniproj_Id == iniproj_Id);
                dao.Delete<Dr_Ini_Laymate>(p => p.Iniproj_Id == iniproj_Id);

                foreach (var item in dict)
                {
                    Dr_Ini_Stru stru = new Dr_Ini_Stru();
                    stru.Inistru_Id = MyApp.Locator.GetInstance<IKeyGenrateService>().NewCombStruId(iniproj_Id, item.Key.ToString());
                    stru.Iniproj_Id = iniproj_Id;
                    stru.Stru_Combi = item.Key;
                    stru.Layer_Thick = item.Value;
                    dao.Save<Dr_Ini_Stru>(stru);
                }
                dbTrans.Commit();
            }
        }


        public void SaveMaterials<T>(string iniproj_Id, string hotmonth, string exetem, string annurain, IEnumerable<T> laymateList)
            where T : IDr_Ini_Laymate
        {
            ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
            using (IDbTransaction dbTrans = dao.BeginTransaction())
            {
                dao.Dr_Largemeas_Ini.UpdateMaterils(iniproj_Id, hotmonth, exetem, annurain);
                dao.SaveAll<T>(laymateList);
                dbTrans.Commit();
            }
        }
    }
}
